﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;

namespace IncrementalBackups.Helpers
{
    public static class Helper
    {
        /// <summary>
        /// Writes the data table to CSV.
        /// </summary>
        /// <param name="dt">The dt.</param>
        /// <param name="fileName">Name of the file.</param>
        public static void WriteDataTableToCSV(DataTable dt, string fileName)
        {
            StringBuilder str = new StringBuilder();
            foreach (DataRow dr in dt.Rows)
            {
                foreach (object field in dr.ItemArray)
                {
                    str.Append(field.ToString() + "|");
                }
                str.Replace("|", Environment.NewLine, str.Length - 1, 1);
            }
            try
            {
                File.WriteAllText(fileName, str.ToString());
            }
            catch (Exception ex)
            {
                LogsHelper.WriteToErrorLog(ex, "Error convert to data table to csv file", false);
            }
        }
        public static double ConvertBytesToMegabytes(long bytes)
        {
            return (bytes / 1024f) / 1024f;
        }

        public static double ConvertKilobytesToMegabytes(long kilobytes)
        {
            return kilobytes / 1024f;
        }




    }
}
